package com.weilele.leisure.time.ui.module.history

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.FragmentContainerActivity
import com.weilele.base.library.SwitchView
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.db.CommonDb
import com.weilele.leisure.time.databinding.ActivityHistoryVideoBinding
import com.weilele.leisure.time.ui.holder.HistoryVideoHolder
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.livedata.appCompatActivity
import com.weilele.mvvm.utils.safeGet
import com.weilele.mvvm.view.SimpleSwitchView
import com.weilele.leisure.time.bean.db.HistoryVideoBean

class HistoryVideoFragment : BaseBindingFragment<ActivityHistoryVideoBinding>() {
    companion object {
        fun start(activity: AppCompatActivity) {
            FragmentContainerActivity.start<HistoryVideoFragment>(activity)
        }
    }

    private val adpter by lazy {
        HistoryVideoHolder.newInstance()
    }

    override fun onResume() {
        super.onResume()
        updateHistory()
    }

    private fun updateHistory() {
        val datas = CommonDb.getHistoryPlayVideos()
        if (datas.isNullOrEmpty()) {
            showOtherView(
                SwitchView.OtherBean(
                    getString(R.string.no_history_hint),
                    R.drawable.icon_empty
                )
            )
        } else {
            showContentView()
            adpter.refreshList(datas)
        }
    }


    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.rcv.adapter = adpter
        appCompatActivity?.title = getString(R.string.history_video)
        enableSwipeDelete()
    }

    override fun isNeedToolbar(): Boolean {
        return true
    }

    private fun enableSwipeDelete() {
        ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
            0,
            ItemTouchHelper.LEFT
                    or ItemTouchHelper.RIGHT
        ) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
//                val fromPos = viewHolder.bindingAdapterPosition
//                val toPos = target.bindingAdapterPosition
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                viewHolder.safeGet<MvvmHolder<HistoryVideoBean, *>>()?.let { holder ->
                    val index = holder.bindingPosition
                    val data = adpter.getItem(index)
                    adpter.removeItem(index)
                    CommonDb.deleteHistory(data)
                }
                if (adpter.currentList.isNullOrEmpty()) {
                    showOtherView(
                        SwitchView.OtherBean(
                            getString(R.string.no_history_hint),
                            R.drawable.icon_empty
                        )
                    )
                }
            }
        }).apply {
            this.attachToRecyclerView(mBinding.rcv)
        }
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }
}